package com.globallogic.socialnetwork.jsf.bean;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;

import com.globallogic.socialnetwork.entity.User;
import com.globallogic.socialnetwork.service.UserService;
import com.globallogic.socialnetwork.util.DaoException;
import com.globallogic.socialnetwork.util.WebApplication;

/**
 * JSF Bean to handle all functions associated with login
 * 
 */
@Named("loginBean")
@Scope("session")
public class LoginBean {

	@Inject
	@Named("userService")
	private UserService userService;
	private User user;
	@NotEmpty(message = "Field is required")
	private String login;
	@NotEmpty(message = "Field is required")
	private String password;

	private final static Logger log = Logger
			.getLogger("SocialNetworkFileLogger");

	public LoginBean() {
	}

	/**
	 * Checks if user with such login and password exists in database
	 * 
	 * @return "home" if user was found or empty string if wasn't
	 */
	public String authorization() {
		if (StringUtils.isEmpty(login) || StringUtils.isEmpty(password)) {
			return "";
		} else {
			try {
				user = userService.login(login, password);
				if (user == null) {
					try {
						WebApplication
								.addMessage("Wrong login and/or password.");
					} catch (NullPointerException e) {}
					return "";
				}
				try {
					WebApplication.setSessionAttribute("userlogin",
							user.getLogin());
				} catch (NullPointerException e) {
				}
				clearForm(false);
			} catch (DaoException e) {
				log.warn(e);
			}
			return "home";
		}
	}

	/**
	 * Invalidates current session, clears form
	 * 
	 * @return "logout"
	 */
	public String logout() {
		try{
			WebApplication.invalidateSession();
		}catch (NullPointerException e) {	}
		clearForm(true);
		return "logout";
	}

	/**
	 * Clears login form
	 */
	public void clearForm(boolean flag) {
		if (flag) {
			user = null;
		}
		login = "";
		password = "";
	}

	/**
	 * Updates user
	 * 
	 * @return "home"
	 */
	public String updateUser() {
		try {
			userService.updateUser(user);
		} catch (DaoException e) {
			log.warn(e);
			return "";
		}catch (HibernateOptimisticLockingFailureException e) {
			log.warn(e);
			return "";
		}
		return "home";
	}

	/**
	 * 
	 * @return user by login
	 */
	public User getUser() {
		try {
			user = userService.getUser(user.getLogin());
		} catch (DaoException e) {
			log.warn("");
		}
		return user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
